//
//  PreCompareViewController.m
//  CPU_Utility
//
//  Created by leonardo on 15/10/23.
//  Copyright © 2015年 leonardo. All rights reserved.
//

#import "PreCompareViewController.h"
#import "CompareTableViewCell.h"
#import "CompareViewController.h"
@interface PreCompareViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UIButton *btnStart;

@end

@implementation PreCompareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.rowHeight = 80;
    
    [_btnStart addTarget:self action:@selector(btnStartClicked) forControlEvents:UIControlEventTouchUpInside];
    
    
}
-(void)viewWillAppear:(BOOL)animated{
    [_myTableView reloadData];
}
-(void)btnStartClicked{
    CompareViewController * vc = [CompareViewController new];
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[[NSUserDefaults standardUserDefaults] arrayForKey:COMPARE_CPU_IDs] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cell";
    CompareTableViewCell * cell = [_myTableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CompareTableViewCell" owner:self options:nil]lastObject];
    }
    NSArray * arrNames = [[NSUserDefaults standardUserDefaults] arrayForKey:COMPARE_CPU_NAMEs];
    NSArray * arrImages = [[NSUserDefaults standardUserDefaults] arrayForKey:COMPARE_CPU_IMAGEs];
    NSArray * arrProIDs = [[NSUserDefaults standardUserDefaults] arrayForKey:COMPARE_CPU_IDs];
    
    [cell configeWithName:arrNames[indexPath.row] image:arrImages[indexPath.row] id:arrProIDs[indexPath.row]];
    cell.deleteCellBlock = ^(NSString * proID){
        [self deleteRow:indexPath];
        
    };
    
    return cell;
    
    
}

-(void)deleteRow:(NSIndexPath *) indexPath {
    NSLog(@"%li",(long)indexPath.row);
    NSMutableArray *  cpuIDs = [[[NSUserDefaults standardUserDefaults] objectForKey:COMPARE_CPU_IDs] mutableCopy];
    if (!cpuIDs) {
        cpuIDs = [NSMutableArray new];
    }
    [cpuIDs removeObjectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults] setObject:cpuIDs forKey:COMPARE_CPU_IDs];
    
    NSMutableArray * cpuNames = [[[NSUserDefaults standardUserDefaults] objectForKey:COMPARE_CPU_NAMEs] mutableCopy];
    if (!cpuNames) {
        cpuNames = [NSMutableArray new];
    }
    [cpuNames removeObjectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults] setObject:cpuNames forKey:COMPARE_CPU_NAMEs];
    
    NSMutableArray * cpuImages = [[[NSUserDefaults standardUserDefaults] objectForKey:COMPARE_CPU_IMAGEs]   mutableCopy];
    if (!cpuImages) {
        cpuImages = [NSMutableArray new];
    }
    [cpuImages removeObjectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults] setObject:cpuImages forKey:COMPARE_CPU_IMAGEs];
    [_myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
    [_myTableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
